import { createRouter, createWebHistory } from 'vue-router'
//生成一个根据views文件夹下文件自动生成路由的函数
function generateRoutes() {
  const views = import.meta.glob('@/views/**/*.vue')
  for (const path in views) {
    // 使用白名单验证路径格式
    const pathPattern = /^.*\/views\/(.*)\.vue$/;
    const match = path.match(pathPattern);

    if (!match) {
      console.warn(`Invalid view path format: ${path}`);
      continue;
    }

    // 安全地提取路径名
    const routePath = '/' + match[1];
    const routeName = match[1];

    // 使用Object.create(null)创建没有原型的对象
    const route = Object.create(null);
    route.path = routePath;
    route.name = routeName;
    route.component = views[path];

    router.addRoute(route);
  }
}

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'HomePage',
      component: () => import('@/views/HomePage/index.vue')
    }
  ]
})
generateRoutes()

export default router
